import { Component, director, _decorator, Node, Label, tween, Vec3, v3, game, Event, Sprite, SpriteFrame, Tween, math } from "cc";
import SoundManager from "../Manager/SoundManager";
import { GameManager } from "../Manager/GameManager";
import { GameData } from "../Data/GameData";
import Banner from "../Tools/Banner";
import { Login } from "../../_RealName/Login";

const { ccclass, property } = _decorator;

@ccclass('GameUI')
export default class GameUI extends Component {
    private static instance: GameUI;
    static get Instance() {
        return this.instance;
    }

    @property(Node)
    victoryPanel: Node;
    @property(Node)
    failPanel: Node;
    @property(Node)
    gamePanel: Node;
    @property(Node)
    rotatePanel: Node;
    @property(Node)
    answerPanel: Node;
    @property(Label)
    timeLabel: Label;
    @property(Node)
    answer: Node;
    @property(Sprite)
    answerIcon: Sprite;
    @property([SpriteFrame])
    answerSf: SpriteFrame[] = [];

    @property(Node)
    TipsNode: Node = null;
    @property(Node)
    HandNode: Node = null;

    time = 30;
    isFinish = false;
    isTip = false;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        game.frameRate = 59;
        GameUI.instance = this;
    }

    start() {
        this.schedule(() => {
            if (!this.isFinish) {
                this.time--;
                this.timeLabel.string = this.time.toString();
                if (this.time == 0) this.Fail();
            }
        }, 1);
    }

    showTips() {
        this.TipsNode.active = true;
        tween(this.HandNode)
            .by(0.8, { position: v3(0, 500, 0) }, { easing: `sineOut` })
            .by(0.8, { position: v3(0, -500, 0) }, { easing: `sineOut` })
            .delay(0.5)
            .union()
            .repeatForever()
            .start();
    }

    hideTips() {
        this.TipsNode.active = false;
        Tween.stopAllByTarget(this.HandNode);
    }

    Victory() {
        if (this.isFinish) return;
        GameManager.Instance.SetQuat();
        SoundManager.Instance.PlayWin();
        this.gamePanel.active = false;
        this.isFinish = true;
        const index = GameData.Cleared.indexOf(GameData.levelNum);
        if (index == -1) {
            GameData.Cleared.push(GameData.levelNum);
            Login.Instance.updateDataByName();
        }
        if (GameData.levelNum == GameData.Unlock) {
            GameData.Unlock = math.clamp(GameData.Unlock + 1, 1, 100);
            Login.Instance.updateDataByName();
        }
        this.scheduleOnce(() => {
            this.victoryPanel.active = true;
        }, 2);
    }

    Fail() {
        if (this.isFinish) return;
        // AndroidTool.ShowAD(ADEnum.ChaPing);
        SoundManager.Instance.PlayFail();
        this.gamePanel.active = false;
        this.isFinish = true;
        this.failPanel.active = true;
    }

    Next() {
        SoundManager.Instance.PlayButton();
        GameData.levelNum++;
        if (GameData.levelNum <= 100) {
            GameData.scene = "GameScene";
            director.loadScene("LoadScene");
        }
        else director.loadScene("MainScene");

    }

    Reset() {
        SoundManager.Instance.PlayButton();
        GameData.scene = "GameScene";
        director.loadScene("LoadScene");
    }

    Back() {
        director.loadScene("MainScene");
        SoundManager.Instance.PlayButton();
    }

    Tip() {
        SoundManager.Instance.PlayButton();
        this.answerPanel.active = true;
    }

    Answer() {
        SoundManager.Instance.PlayButton();
        Banner.Instance.CreateVideo(this.AnswerReward);
    }

    AnswerReward() {
        var self = GameUI.Instance;
        self.answer.active = true;
        self.answerPanel.children[0].children[1].active = false;
        self.answerIcon.spriteFrame = self.answerSf[GameData.levelNum - 1];
    }

    AnswerClose() {
        this.answerPanel.active = false;
        SoundManager.Instance.PlayButton();
    }

    Rotate() {
        if (this.isFinish) return;
        if (GameManager.Instance.isRotate) return;
        this.rotatePanel.active = false;
        GameManager.Instance.Rotate();
        SoundManager.Instance.PlayButton();
    }
}
